package ssq.utils.wolfmankill;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.Arrays;

import javax.swing.JOptionPane;

import net.sf.json.JSONObject;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import ssq.utils.Base64Utils;

public class WSClient extends WebSocketClient
{
    ClientGUI         gui     = ClientGUI.getInstance();
    Participant       me;
    protected boolean canVote = false;

    public WSClient(URI serverUri, String name)
    {
        super(serverUri);

        me = new Participant(name);
    }

    @Override
    public void onClose(int arg0, String arg1, boolean arg2)
    {
        gui.enableReconnect();
        gui.appendMessage("链接已经断开");
    }

    @Override
    public void onError(Exception arg0)
    {
        arg0.printStackTrace();
        JOptionPane.showMessageDialog(ClientGUI.getInstance(), arg0.getLocalizedMessage());
    }

    @Override
    public void onMessage(String msg)
    {
        JSONObject json;
        try
        {
            json = JSONObject.fromObject(msg);
        }
        catch (Exception e1)
        {
            return;
        }

        String type = "";
        try
        {
            type = json.getString("type");
        }
        catch (Exception e)
        {
        }

        msg = "";
        try
        {
            msg = json.getString("msg");
        }
        catch (Exception e)
        {
        }

        String from = "";
        try
        {
            from = json.getString("from");
        }
        catch (Exception e)
        {
        }

        switch (type)
        {
            case "msg":
            {
                try
                {
                    gui.appendMessage(from + " : " + new String(Base64Utils.decode(msg), "UTF-8"));
                }
                catch (UnsupportedEncodingException e)
                {
                }
            }
            break;

            case "queryInfo":
            {
                String hint = json.getString("hint");
                String backType = json.getString("backType");

                String result = JOptionPane.showInputDialog(hint);
                if (result == null)
                {
                    result = "";
                }

                JSONObject resultJson = new JSONObject();

                resultJson.put("type", backType);
                resultJson.put("msg", result);

                send(resultJson.toString());
            }
            break;

            case "fromJson":
            {
                JSONObject tmp = json.getJSONObject("msg");
                me.fromJson(tmp);
                gui.refreshState();
            }
            break;

            case "players":
            {
                gui.setNames(Arrays.asList(msg.replace("[", "").replace("]", "").split(",")));
            }
            break;
            
            case "toggleVote":
            {
                canVote = !canVote;
            }
            break;
            
            default:
            {
                try
                {
                    ClientGUI.class.getMethod(type, String.class).invoke(gui, msg);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
            break;
        }
    }

    @Override
    public void onOpen(ServerHandshake arg0)
    {
        register();
        gui.reset();
        gui.disableReconnect();
    }

    private void register()
    {
        send(WSServer.constructMessage("register", me.name));
    }

    public String toDetailedString()
    {
        StringBuilder sb = new StringBuilder(me.toFullString());
        sb.append("\n canVote: ").append(canVote);
        return null;
    }
}
